package com.amos.cmp_android.ui;

import android.content.Intent;
import android.net.Uri;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.amos.cmp_android.R;
import com.amos.cmp_android.constant.URLConstant;
import com.amos.cmp_android.model.NewsModel;
import com.amos.cmp_android.net.STError;
import com.amos.cmp_android.net.STNetTask;
import com.amos.cmp_android.presenter.GetNewsPresenter;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.DraweeView;

import java.sql.Timestamp;
import java.util.ArrayList;

/**
 * Created by Amos on 2016/4/13.
 */
public class NewsActivity extends BaseActivity implements
        STNetTask.BeforeQueryHook, STNetTask.AfterQueryHook, GetNewsPresenter.GetNewsListener,
        SwipeRefreshLayout.OnRefreshListener, BaseRecyclerAdapter.OnItemClickListener{

    private GetNewsPresenter presenter;
    private BaseRecyclerAdapter<NewsModel> adapter;
    private ArrayList<NewsModel> mList;
    private DraweeView draweeView;

    @Override
    protected String getToolbarTitle() {
        return "新闻浏览";
    }

    @Override
    protected void initData() {
        super.initData();



        mList = new ArrayList<>();

        swipeRefresh.setColorSchemeResources(R.color.blue, R.color.gray, R.color.green, R.color.red);//设置跑动的颜色值
        swipeRefresh.setOnRefreshListener(this);
        adapter = new BaseRecyclerAdapter<NewsModel>(this, mList) {
            @Override
            public int getItemLayoutId(int viewType) {
                return R.layout.recycler_item_news;
            }
            @Override
            public void bindData(RecyclerViewHolder holder, int position, NewsModel item) {

                draweeView = (DraweeView) holder.getView(R.id.recycler_item_image);
                draweeView.setController(Fresco.newDraweeControllerBuilder().
                        setUri(Uri.parse(URLConstant.HOST + item .getNurl())).
                        setAutoPlayAnimations(true).build());
                holder.setText(R.id.recycler_item_title, item.getTitle())
                        .setText(R.id.recycler_item_category, item.getCategory())
                        .setText(R.id.recycler_item_time, (new Timestamp(item.getNtime()).toString().substring(0,10)))
                .setText(R.id.recycler_item_content,item.getContent());
            }
        };
        adapter.setOnItemClickListener(this);
        recyclerView.setAdapter(adapter);
        presenter = new GetNewsPresenter(this);
        presenter.setBeforeQueryHook(this);
        presenter.setAfterQueryHook(this);
        presenter.setListener(this, mList);
        presenter.getNews(1,50);
    }

    @Override
    public void beforeQueryHook() {
        if (swipeRefresh != null && !swipeRefresh.isRefreshing())
            swipeRefresh.setRefreshing(true);
    }

    @Override
    public void afterQueryHook() {
        if (swipeRefresh != null && swipeRefresh.isRefreshing())
            swipeRefresh.setRefreshing(false);
    }

    @Override
    public void onSuccess() {
        if (adapter != null)
            adapter.notifyDataSetChanged();
        if (swipeRefresh != null && swipeRefresh.isRefreshing())
            swipeRefresh.setRefreshing(false);
        Toast.makeText(this, "加载成功", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onFailed(STError error) {
        Toast.makeText(this, "加载失败", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onItemClick(View itemView, int pos) {
        NewsModel model = mList.get(pos);
        Intent intent = new Intent(NewsActivity.this, ShowNewsActivity.class);
        intent.putExtra("picUrl", model.getNurl());
        intent.putExtra("content", model.getContent());
        intent.putExtra("title", model.getTitle());
        startActivity(intent);
    }

    @Override
    public void onRefresh() {
        presenter.getNews(1,50);
    }
}
